package fun.joney.controller;


import fun.joney.Main;
import fun.joney.pojo.Info;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.FXCollections;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;

import java.net.URL;
import java.util.Optional;
import java.util.Random;
import java.util.ResourceBundle;

public class First extends GridPane implements Initializable {
    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;
    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML private Button searchBtn;
    @FXML private Button nextPage;
    @FXML private ListView<String> listView;
    @FXML private TableView<Info> viewTb;
    @FXML private TableColumn<Info,Integer> id;
    @FXML private TableColumn<Info,String> name;
    @FXML private TableColumn<Info,String> addr;
    @FXML private TableColumn<Info,String> createTime;

    Alert alert;
    Alert confirm;
    private int selectedIndex = -1;

    private Main application;

    public void setApp(Main application){
        this.application = application;
    }

    @FXML
    public void serarchData(ActionEvent event){
        System.out.println("sdfsdfsfsdf");
        if(event.getTarget() instanceof Button){
            String btnId = ((Button) event.getTarget()).getId();
            System.out.println(btnId);
        }
        Info info = new Info(new Random().nextInt(1000),"张三","广东省深圳市南山区","2018-06-06 18:00:00");
        ObservableList<Info> items = viewTb.getItems();
        if(items == null || items.size()==0){
            items = FXCollections.observableArrayList(info);
        }else{
            items.add(info);
        }
        viewTb.setItems(items);
    }

    @FXML
    public void alert(MouseEvent event){
        alert.showAndWait(); //显示弹窗，同时后续代码等挂起
    }
    @FXML
    public void confirm(MouseEvent event){
        String msg = null;
        Optional<ButtonType> result = confirm.showAndWait();//显示弹窗，同时后续代码等挂起
        if(result.isPresent() && result.get() == ButtonType.OK){
            msg = "你点击了确认";
        }else{
            msg = "你点击了取消";
        }
        log(msg);
    }
    //@FXML
    public void clickRow(MouseEvent event){
        Object obj = event.getTarget();
        TableView.TableViewSelectionModel<Info> model = viewTb.getSelectionModel();
        Info info = model.getSelectedItem();
        if(obj instanceof TableCell){
            TableCell cell = (TableCell) obj;
            if(!cell.isEmpty()){
                if(info != null){
                    if(selectedIndex != model.getSelectedIndex()){
                        selectedIndex = model.getSelectedIndex();
                        System.out.println("FocusedIndex："+model.getFocusedIndex());
                        System.out.println("selectedIndex："+model.getSelectedIndex());
                        System.out.println("选中的行为："+info.toString());

                    }
                }
            }
        }else{
            if(selectedIndex != model.getSelectedIndex()){
                selectedIndex = model.getSelectedIndex();
                System.out.println("selectedIndex："+model.getSelectedIndex());
                System.out.println("选中的行为："+info.toString());
            }
        }

    }
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        System.out.println(location.getPath());
        id.setCellValueFactory(new PropertyValueFactory("id"));
        name.setCellValueFactory(new PropertyValueFactory("name"));
        addr.setCellValueFactory(new PropertyValueFactory("addr"));
        createTime.setCellValueFactory(new PropertyValueFactory("createTime"));

        viewTb.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        viewTb.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {   //Lambda 表达式
            System.out.println("oldValue:"+oldValue+"  newValue:"+newValue);
            log(viewTb.getSelectionModel().getSelectedItem().toString());
        });


        alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("提示"); //设置标题，不设置默认标题为本地语言的information
        alert.setHeaderText("这是一个弹出窗口"); //设置头标题，默认标题为本地语言的information
        alert.setContentText("Welocme to JavaFX");

        confirm = new Alert(Alert.AlertType.CONFIRMATION);
        confirm.setTitle("提示"); //设置标题，不设置默认标题为本地语言的information
        confirm.setHeaderText("这是一个弹出窗口"); //设置头标题，默认标题为本地语言的information
        confirm.setContentText("Welocme to JavaFX");

        ObservableList<String> strList = FXCollections.observableArrayList("操作日志","sdfsdf");
        //listView = new ListView<>();
        listView.setItems(strList);
        listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
        //list的选中项切换事件
        listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                String v = listView.getSelectionModel().getSelectedItem();
                System.out.println(v);
            }
        });
        //设置选中行
        listView.getSelectionModel().clearAndSelect(0);
        listView.setEditable(true);
        //listView.setCellFactory(ComboBoxListCell.forListView(strList));

        nextPage.setOnMouseClicked(event ->{
            log("to next page");
            application.gotoSecond();
        });
    }

    public void log(String msg){
        ObservableList<String> items = listView.getItems();
        items.add(msg);
        //listView.setItems(items);

    }
}
